package la.iok.hzsvn.lewin.movie.operation.controller;

import la.iok.hzsvn.lewin.movie.operation.entity.ChargeOrder;
import la.iok.hzsvn.lewin.movie.operation.service.ChargeOrderService;
import la.iok.hzsvn.lewin.movie.operation.service.PayService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("wx")
public class WxNotifyController {
    private final PayService payService;
    private final ChargeOrderService chargeOrderService;

    public WxNotifyController(@Qualifier("wxPayService") PayService payService,
                              ChargeOrderService chargeOrderService) {
        this.payService = payService;
        this.chargeOrderService = chargeOrderService;
    }

    @RequestMapping("notify")
    public void notifyCallBack(@RequestBody String notification,
                                 @RequestHeader("Wechatpay-Signature") String signature,
                                 @RequestHeader("Wechatpay-Timestamp") String timestamp,
                                 @RequestHeader("Wechatpay-Nonce") String nonce,
                                 @RequestHeader("Wechatpay-Serial") String serialNum){
        String orderId = payService.notify(notification,signature,serialNum,nonce,timestamp);
        if(orderId.startsWith("w_")){
            //钱包充值

        }else{
            ChargeOrder chargeOrder = new ChargeOrder();
            chargeOrder.setUuid(orderId);
            payService.queryOrder(chargeOrder);
            chargeOrderService.update(chargeOrder);
        }
    }
}
